package com.skymxc.skin

import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.ViewCompat
import java.util.ArrayList

/**
 * <p>
 * 管理皮肤包可以更换的属性
 * </p>
 *
 * @author 孟祥超
 * <p>
 * date: 2021/9/20  11:04 下午
 */
class SkinAttribute {
    private val TAG = "SkinAttribute"
    companion object {
        private val mAttributes = arrayOf(
            "background",
            "src",
            "textColor",
            "drawableLeft",
            "drawableTop",
            "drawableRight",
            "drawableBottom"
        )
    }

    //记录换肤需要操作的View与属性信息
    private val mSkinViews: ArrayList<SkinView> = ArrayList()


    /**
     * 记录 哪个 View 需要换肤
     */
    fun look(view:View,attrs:AttributeSet){
        val skinPairs = arrayListOf<SkinPair>()
        for (i in 0 until attrs.attributeCount){
            //获得属性名  textColor/background
            val attributeName = attrs.getAttributeName(i)
            if (mAttributes.contains(attributeName)){
                //包含需要换肤的属性
                val attributeValue = attrs.getAttributeValue(i)
                if (attributeValue.startsWith("#")){
                    //比如color 以#开头表示写死的颜色 不可用于换肤
                    continue
                }
                val resId = if (attributeValue.startsWith("?")){
                    // 主题属性以 ？ 开头
                    val attrId = attributeValue.substring(1).toInt()
                    getResId(view.context, IntArray(attrId))[0]
                }else{
                    // 正常以 @ 开头
                    attributeValue.substring(1).toInt()
                }
                val skinPair = SkinPair(attributeName, resId)
                Log.i(TAG, "look: $skinPair")
                skinPairs.add(skinPair)
            }
        }

        if (skinPairs.isNotEmpty() || view is SkinViewSupport){
            val skinView = SkinView(view,skinPairs)
            // // 如果选择过皮肤 ，调用 一次 applySkin 加载皮肤的资源
            skinView.applySkin()
            mSkinViews.add(skinView)
        }
    }

    /*
       对所有的view中的所有的属性进行皮肤修改
     */
    fun applySkin() {
        for (mSkinView in mSkinViews) {
            mSkinView.applySkin()
        }
    }

    class SkinPair(
        val attributeName: String, //属性名字
        val resId: Int//对应的资源 ID

    ) {
        override fun toString(): String {
            return "SkinPair(attributeName='$attributeName', resId=$resId)"
        }
    }

    class SkinView(
        val view: View,
        val skinPairs: List<SkinPair> //这个View的能被 换肤的属性与它对应的id 集合
    ) {
        private val TAG ="SkinView"
        fun applySkin() {
            applySkinSupport()
            skinPairs.forEach {
                var left: Drawable? = null
                var top: Drawable? = null
                var right: Drawable? = null
                var bottom: Drawable? = null
                when (it.attributeName) {
                    "background" -> {
                        //设置 view 的 background
                        val background = SkinResources.getInstance().getBackground(it.resId)
//                        Log.i(TAG, "applySkin: view:$view ->background:$background ")
                        //背景可能是 @color 也可能是 @drawable
                        if (background is Drawable) {
//                            Log.i(TAG, "applySkin: background is drawable")
                            ViewCompat.setBackground(view, background)
                        } else {
                            view.setBackgroundColor(background as Int)
                        }
                    }
                    "src" -> {
                        val background = SkinResources.getInstance().getBackground(it.resId)
                        if (background is Drawable) {
                            val imageView = view as ImageView
                            imageView.setImageDrawable(background)

                        } else {
                            val imageView = view as ImageView
                            imageView.setImageDrawable(ColorDrawable(background as Int))
                        }
                    }
                    "textColor" -> {
                        val textView = view as TextView
                        val color = SkinResources.getInstance().getColor(it.resId)
                        Log.i(TAG, "applySkin: $view ->textColor:$color")
                        textView.setTextColor(color)
                    }
                    "drawableLeft" -> {
                        left = SkinResources.getInstance().getDrawable(it.resId)
                    }
                    "drawableTop" -> {
                        top = SkinResources.getInstance().getDrawable(it.resId)
                    }
                    "drawableRight" -> {
                        right = SkinResources.getInstance().getDrawable(it.resId)
                    }
                    "drawableBottom" -> {
                        bottom = SkinResources.getInstance().getDrawable(it.resId)
                    }
                }
                if (null != left || null != right || null != top || null != bottom) {
                    (view as TextView).setCompoundDrawablesWithIntrinsicBounds(
                        left, top, right,
                        bottom
                    )
                }
//                Log.i(TAG, "applySkin: $view->${it.attributeName}")
            }

        }

        private fun applySkinSupport() {
            if (view is SkinViewSupport) {
                view.applySkin()
            }
        }
    }

}